`
https://leetcode.cn/problems/path-sum-ii/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {number[][]}
 */
var pathSum = function (root, targetSum) {
  const res = []
  const path = []
  let sum = 0

  const traverse = (root) => {
    if (root === null) return

    sum += root.val
    path.push(root.val)

    // 如果是叶子结点，判断是否符合条件
    if (root.left === null && root.right === null) {
      if (sum === targetSum) {
        res.push(path.slice())
      }
    }

    traverse(root.left)
    traverse(root.right)

    path.pop()
    sum -= root.val
  }

  traverse(root)

  return res
};